package operation;

import book.Book;
import book.BookList;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: 李白
 * @Created: 2024/8/19 21:16
 */
public class AddOperation implements IOPreparation{
    public void work(BookList bookList){//子类必须要先构造父类的构造方法，才能进行自己的构造方法
        System.out.println("请输入书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入作者");
        String author = scanner.nextLine();
        System.out.println("价格");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("类型");
        String type = scanner.nextLine();
        Book book = new Book(name,author,price,type);
        int currentSize = bookList.getSize();
        //看是否还能添加
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);//循环体内部的变量是与外部的变量相通的
            if (book1.getName().equals(name)){
                System.out.println("图书重复，不可添加");
                return;
            }
        }
        //书架满
        if (currentSize == bookList.getBooks().length){
            System.out.println("书架已满，不能再添加");
        }else{
            bookList.setBook(currentSize,book);
            bookList.setUsedSize(currentSize+1);
            System.out.println("添加成功");
            for (int j = 0; j < bookList.getSize(); j++) {
                Book book1 = bookList.getBook(j);
                System.out.println(book1);
            }
        }

    }
}
